/**
 * Copyright 2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * ChunMap是开源软件。你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。
 * 
 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 * 
 * 下面此段程序作者YangJianDong(chunquedong)
 * 最后修改时间2010.7.17
 */
package chunmap.view.render;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.font.TextAttribute;
import java.text.AttributedString;

import chunmap.data.feature.Feature;
import chunmap.data.feature.Shape;
import chunmap.model.coord.Position;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.Geometry;
import chunmap.view.View;

public class LabelSymbol extends Symbol{

	public Paint stringColor = Color.black;
	public int labelHide = 100;
	
	@Override
	public void draw(Graphics g, Feature f, View view) {
		if(f instanceof Shape){
			Shape shp=(Shape)f;
			drawLabel(g,shp.getGeometry(),shp.getLabel(),view);
		}
	}
	
	private void drawLabel(Graphics g, Geometry geo, String text, View view) {
		if (text == null || text == "") return;
		
		Envelope env=geo.getEnvelop().transform(view.world2Screen());
		if(env.getWidth()+env.getHeight()<labelHide)return;
		
		AttributedString astring = new AttributedString(text);
		astring.addAttribute(TextAttribute.FOREGROUND, stringColor);
		Position p = env.getCenter();
		g.drawString(astring.getIterator(), (int) p.getX(), (int) p.getY());
	}
}
